package org.lex.swing.plaf.widget;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JMenu;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.plaf.metal.MetalLookAndFeel;
import javax.swing.plaf.metal.MetalTheme;

import org.lex.swing.plaf.util.LookAndFeelUtils;

public class MetalMenu extends JMenu {
	private Component[] components = null;
	private MetalLookAndFeel metal = null;
	private MetalTheme[] metalThemes = null;
	private JRadioButtonMenuItem[] childrenMenuItems = null;

	public MetalMenu() {
		super();
		this.metal = new MetalLookAndFeel();
		this.setText(metal.getName());
		metalThemes = LookAndFeelUtils.getMetalThemes();
		childrenMenuItems = new JRadioButtonMenuItem[metalThemes.length];
		JRadioButtonMenuItem item = null;
		for (int i = 0; i < metalThemes.length; i++) {
			item = new ThemeRadioMenuItem(metalThemes[i]);
			childrenMenuItems[i] = item;
			this.add(item);
		}
	}

	public MetalMenu(Component... cs) {
		this();
		this.components = cs;
	}

	JRadioButtonMenuItem[] getChildern() {
		return childrenMenuItems;
	}

	class ThemeRadioMenuItem extends JRadioButtonMenuItem implements ActionListener {
		private MetalTheme theme = null;

		ThemeRadioMenuItem(MetalTheme theme) {
			super(theme.getName());
			this.theme = theme;
			this.addActionListener(this);
		}

		public void actionPerformed(ActionEvent e) {
			if (false == this.isSelected())
				return;
			MetalLookAndFeel.setCurrentTheme(theme);
			LookAndFeelUtils.updateAllComponentsLookAndFeel(metal, components);
		}
	}
}
